package router

import (
	"encoding/base64"

	"gddgame.cc/galaxy/asteroid"
	"gddgame.cc/galaxy/asteroid/cluster"
	"gddgame.cc/galaxy/binocle"
	. "gddgame.cc/galaxy/common/dsl/db"
	. "gddgame.cc/galaxy/common/dsl/web"
	//"gddgame.cc/galaxy/common/service/pathfinding"
	"gddgame.cc/galaxy/core/routers"
	"gddgame.cc/galaxy/project/breeder/breeder/controller"
	"gddgame.cc/galaxy/server/composer"
	"gddgame.cc/galaxy/server/web"
	"gddgame.cc/galaxy/utils/def"
)

func SystemHandler(path string, builder composer.Builder, router web.Router) {
	var agent binocle.App
	builder.Factory("agent", &agent)

	r := NewSwaggerRouter(path, builder, router, &SwaggerRouterConfig{
		Info: SwaggerInfo{
			Title: "",
		},
	})

	web.Restful(router)
	routers.ManageRouterBind(builder, r)
	controller.Env.BindRouter(r)

	agent.MountClient(func(client *cluster.Client) {
		client.On("map/reload", func(message asteroid.Message) {
			controller.ReloadMap(client.CurrentNode())
		})
	})
	router.Put("/map/reload", func(context web.Context, next web.Next) error {
		client := agent.Client()
		client.All().Send("map/reload")
		return next(nil)
	})
	r.Post("/map/create", Swagger{
		Controller: func(context def.Context, userId string, name string, content string, people int, aiNumber int, data string) (int, error) {
			b, err := base64.StdEncoding.DecodeString(data)
			if err != nil {
				return 0, err
			}
			mapItem, err := controller.CreateMap(context, 0, name, content, people, aiNumber, b)
			if err != nil {
				return 0, err
			}
			return mapItem.Id, err
		},
		Params: []Param{
			{Name: `userId`, In: Body, Required: true},
			{Name: `name`, In: Body, Required: true, Type: String},
			{Name: `content`, In: Body, Required: true, Type: String},
			{Name: `people`, In: Body, Required: true, Type: Int},
			{Name: `aiNumber`, In: Body, Required: true, Type: Int},
			{Name: `data`, In: Body, Required: true},
		},
		Response: ApiResponse,
	})
	r.Post("/map/update", Swagger{
		Controller: func(context def.Context, userId string, mapId int, people int, aiNumber int, data string) (int, error) {
			b, err := base64.StdEncoding.DecodeString(data)
			if err != nil {
				return 0, err
			}

			//mapItem, err := controller.GetMap(context, mapId)
			//if err != nil {
			//	fmt.Println("update map", err)
			//	return 0, err
			//}
			//mapData := controller.Env.CooperateManager.ParseMap(b)
			//extendMap := controller.Env.CooperateManager.ParseStrategyMap(mapData.Extend)
			//nodes := make([][]int, len(extendMap.RangeBase))
			//for index, node := range extendMap.RangeBase {
			//	position := pathfinding.DecodeVector2(node.Position)
			//	nodes[index] = []int{int(position.X), int(position.Y)}
			//}
			//if err := controller.UpdateNode(context, mapItem.Id, nodes); err != nil {
			//	fmt.Println("update map", err)
			//	return 0, err
			//}
			_, err = controller.UpdateMap(context, 0, mapId, people, aiNumber, b)
			return mapId, err
		},
		Params: []Param{
			{Name: `userId`, In: Body, Required: true},
			{Name: `mapId`, In: Body, Required: true, Type: Int},
			{Name: `people`, In: Body, Required: true, Type: Int},
			{Name: `aiNumber`, In: Body, Required: true, Type: Int},
			{Name: `data`, In: Body, Required: true},
		},
		Response: ApiResponse,
	})

	r.Post("/test/cooperate", Swagger{
		Controller: func(context def.Context, userId string, mapId int, together bool) (string, error) {
			// 查询已有的地图
			mapItem, err := controller.GetMap(context, mapId)
			if err != nil {
				return "", err
			}
			mapNo, err := controller.CreateNo(context, mapItem)
			if err != nil {
				return "", err
			}
			return mapNo.Id, nil
		},
		Params: []Param{
			{Name: `userId`, In: Body, Required: true},
			{Name: `mapId`, In: Body, Required: true, Type: Int},
			{Name: `together`, In: Body, Required: true, Type: Boolean},
		},
		Response: ApiResponse,
	})

	r.Get("/map/content", Swagger{
		Controller: func(context def.Context) (string, error) {
			return "", nil
		},
		Params:   []Param{},
		Response: ApiResponse,
	})

	_ = TableRouterWith(builder, r.Group("/db"), agent.Name())
	_ = ManagerRouterWith(builder, r.Group("/manager"))
}
